package com.hjy.config;

import com.hjy.interceptor.AuthorizeInterceptor;
import com.hjy.interceptor.LoginInterceptor;
import com.hjy.properties.HjySecurityProperties;
import com.hjy.utils.TokenManager;
import com.hjy.utils.UserDetailHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


@AllArgsConstructor
@Data
@NoArgsConstructor
public class MVConfig implements WebMvcConfigurer {

    @Autowired
    private UserDetailHandler userDetailHandler;
    @Autowired
    private TokenManager tokenManager;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private HjySecurityProperties hjySecurityProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthorizeInterceptor(stringRedisTemplate,hjySecurityProperties)).addPathPatterns("/**").order(100);
        // order 越小, 越先执行
        registry.addInterceptor(new LoginInterceptor(tokenManager,userDetailHandler,stringRedisTemplate,hjySecurityProperties)).addPathPatterns("/**").order(99);
    }
}
